package com.aiyige.page.publish.info.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.eventbus.EventPhotoTap;
import com.aiyige.base.eventbus.EventTextNodeFocusChanged;
import com.aiyige.base.eventbus.EventTextNodeTextChanged;
import com.aiyige.page.publish.info.model.PhotoNode;
import com.aiyige.page.publish.info.model.TextNode;
import com.aiyige.page.publish.info.model.VideoNode;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.utils.InputNumFilter;
import com.aiyige.utils.KeyboardUtil;
import com.aiyige.utils.widget.ZanyEditText;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import net.cachapa.expandablelayout.ExpandableLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhuyakun on 2017/11/10.
 */

public class PublishInfoAdapter extends BaseQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int NODE_TYPE_PHOTO = 1;
    public static final int NODE_TYPE_TEXT = 2;
    public static final int NODE_TYPE_VIDEO = 3;

    public PublishInfoAdapter() {
        super(0, new LinkedList<MultiItemEntity>());
    }

    @Override
    protected int getDefItemViewType(int position) {
        return mData.get(position).getItemType();
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = null;
        switch (viewType) {
            case NODE_TYPE_PHOTO:
                baseViewHolder = new PhotoNodeViewHolder(getItemView(R.layout.photo_node, parent));
                break;
            case NODE_TYPE_TEXT:
                baseViewHolder = new TextNodeViewHolder(getItemView(R.layout.text_node, parent));
                break;
            case NODE_TYPE_VIDEO:
                baseViewHolder = new VideoNodeViewHolder(getItemView(R.layout.video_node, parent));
                break;
        }
        return baseViewHolder;
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        switch (item.getItemType()) {
            case NODE_TYPE_PHOTO:
                ((PhotoNodeViewHolder) helper).bindData((PhotoNode) item);
                break;
            case NODE_TYPE_TEXT:
                ((TextNodeViewHolder) helper).bindData((TextNode) item);
                break;
            case NODE_TYPE_VIDEO:
                ((VideoNodeViewHolder) helper).bindData((VideoNode) item);
                break;
        }
    }

    public class TextNodeViewHolder extends BaseViewHolder {
        TextNode textNode;
        @BindView(R.id.textNodeEt)
        ZanyEditText textNodeEt;

        public TextNodeViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            textNodeEt.setTag(R.id.text_node_view_holder, this);
            textNodeEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    EventBus.getDefault().post(new EventTextNodeFocusChanged(hasFocus));
                }
            });
            textNodeEt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    textNode.setText(s.toString());
                    EventBus.getDefault().post(new EventTextNodeTextChanged());
                }
            });
            textNodeEt.setListener(new ZanyEditText.Listener() {
                @Override
                public void onEmptyDelete() {
                    try {
                        if (getAdapterPosition() > getHeaderLayoutCount()) {
                            int dataPosition = getAdapterPosition() - getHeaderLayoutCount() - 1;
                            if (getItem(dataPosition).getItemType() == PublishInfoAdapter.NODE_TYPE_TEXT) {
                                remove(getAdapterPosition() - getHeaderLayoutCount());
                                notifyItemChanged(getHeaderLayoutCount());
                            }
                        }
                    } catch (Exception e) {

                    }
                }
            });
        }

        public void bindData(TextNode data) {
            if (getAdapterPosition() - getHeaderLayoutCount() == 0 && getData().size() <= 1) {
                textNodeEt.setHint(R.string.please_input);
            } else {
                textNodeEt.setHint("");
            }
            textNode = data;
            textNodeEt.setText(textNode.getText());
            int textStyle;
            switch (data.getTextStyle()) {
                case TextNode.TEXT_STYLE_BIG_TITLE:
                    textStyle = R.style.TextNodeBigTitleStyle;
                    break;
                case TextNode.TEXT_STYLE_MAIN_BODY:
                    textStyle = R.style.TextNodeMainBodyStyle;
                    break;
                case TextNode.TEXT_STYLE_SMALL_TITLE:
                    textStyle = R.style.TextNodeSmallTitleStyle;
                    break;
                default:
                    textStyle = R.style.TextNodeMainBodyStyle;
                    break;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                textNodeEt.setTextAppearance(textStyle);
            } else {
                textNodeEt.setTextAppearance(itemView.getContext(), textStyle);
            }
        }
    }

    public class VideoNodeViewHolder extends BaseViewHolder {
        VideoNode videoNode;
        @BindView(R.id.coverIv)
        ImageView coverIv;

        public VideoNodeViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.deleteBtn);
        }

        public void bindData(VideoNode data) {
            videoNode = data;
            Glide.with(itemView.getContext())
                    .load(videoNode.getCoverUrl())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);
        }
    }

    public class PhotoNodeViewHolder extends BaseViewHolder {
        PhotoNode photoNode;
        @BindView(R.id.photoIv)
        ImageView photoIv;
        @BindView(R.id.introductionEt)
        EditText introductionEt;
        @BindView(R.id.expandableLayout)
        ExpandableLayout expandableLayout;

        public PhotoNodeViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.deleteBtn);
            introductionEt.setFilters(new InputFilter[]{new InputNumFilter(PublishUtil.MAX_PHOTO_DESC_LENGTH, false)});
            introductionEt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    photoNode.setDescription(s.toString());
                }
            });
            introductionEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
                }
            });
        }

        public void bindData(PhotoNode data) {
            photoNode = data;
            if (photoNode.isExpand()) {
                expandableLayout.expand(false);
            } else {
                expandableLayout.collapse(false);
            }

            Glide.with(itemView.getContext())
                    .asBitmap()
                    .load(photoNode.getPhotoUrl())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(photoIv);
            introductionEt.setText(photoNode.getDescription());
        }

        @OnClick({R.id.photoLayout})
        public void onViewClick(View view) {
            switch (view.getId()) {
                case R.id.photoLayout:
                    photoNode.setExpand(true);
                    expandableLayout.expand(false);
                    KeyboardUtil.showKeyboard(itemView.getContext(), introductionEt);
                    break;
            }
        }
    }
}
